#include <iostream>
#include <vector>
using namespace std;

struct TreeNode 
{
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
    
};
int n;
TreeNode* _buildTree(vector<int>& inorder, vector<int>& postorder, int i, int j, int& pos)
{
    if (pos < 0 || i > j) return nullptr;
    TreeNode* root = new TreeNode(postorder[pos]);
    int k = 0;
    for (k = 0; k < n; ++k)
    {
        if (postorder[pos] == inorder[k]) break;
    }
    pos--;
    root->right = _buildTree(inorder, postorder, k + 1, j, pos);
    root->left = _buildTree(inorder, postorder, i, k - 1, pos);
    return root;
}
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder)
{
    n = inorder.size();
    int pos = n - 1;

    return _buildTree(inorder, postorder, 0, n - 1, pos);
}
int main()
{
    vector<int> inorder({ 1 });
    vector<int> postorder({ 1 });
    TreeNode* root = new TreeNode(1);
    buildTree(inorder, postorder);
	return 0;
}